﻿/*
entityDef
--name
--fields
----field
------name
------displayName
------type
------isSearchable
------isInList
------sortable
------isEditable
------isInDetail
*/

Ext.define("EaseWeb.Template.EntityHome", {
    extend: "Ext.panel.Panel",

    requires: [
        'Ext.form.*',
        'Ext.grid.*',
        'Ext.data.*',
        'Ext.toolbar.Paging',
        'Ext.selection.CheckboxModel'],

    model: Ext.ModelManager.getModel(entityDef.name),

    constructor: function (entityDef, config) {
        var gridCols = new Array();
        var searchFields = new Array();
        var editFields = new Array();
        var detailFields = new Array();

        Ext.each(entityDef.fields, function (field) {
            if (field.isSearchable)
                searchFields.push({
                    fieldLabel: field.displayName,
                    name: field.name
                });

            if (field.isInList)
                gridCols.push({ header: field.displayName, dataIndex: field.name,
                    sortable: field.sortable,
                    hidden: (field.hidden ? field.hidden : false)
                });

            if (field.isEditable)
                editFields.push({
                    fieldLabel: field.displayName,
                    name: field.name
                });

            if (field.isInDetail)
                detailFields.push({
                    fieldLabel: field.displayName,
                    name: field.name
                });
        });


        //Init list panel
        var store = new Ext.data.Store({
            autoLoad: true,
            model: this.model,
            pageSize: Ext.isNumber(config.pageSize) ? parseInt(config.pageSize) : 20,
            remoteSort: true

            //            proxy: {
            //                type: "ajax",
            //                url: entityDef.baseUrl,
            //                method: "GET",
            //                simpleSortMode: true,
            //                reader: {
            //                    type: "json",
            //                    root: "rows",
            //                    totalProperty: "totalRecords"
            //                }
            //            }
        });

        var gridCfg = {
            store: store,
            columns: gridCols,
            loadMask: true
        };
        if (config.multiSelect != false)
            gridCfg.selModel = new Ext.selection.CheckboxModel({ mode: "MULTI" });
        if (config.paging)
            config.bbar = new Ext.PagingToolbar({
                store: store,
                displayInfo: false
            });
        var grid = new Ext.grid.Panel(gridCfg);
        this.gridPanel = grid;


        //Init search panel
        var searchPanel = new Ext.form.Panel({
            fieldDefaults: {
                msgTarget: 'side'
            },
            defaultType: 'textfield',
            defaults: {
                anchor: '100%'
            },
            items: searchFields,
            buttons: [{
                text: 'Search'
            }]
        });
        this.searchPanel = searchPanel;


        //Init editing panel
        var editPanel = new Ext.form.Panel({
            hidden: true,
            fieldDefaults: {
                msgTarget: 'side'
            },
            defaultType: 'textfield',
            defaults: {
                anchor: '100%'
            },

            items: editFields,

            buttons: [{
                text: 'Save'
            }, {
                text: 'Cancel'
            }]
        });
        this.editPanel = editPanel;


        //Init detail panel
        var detailPanel = new Ext.grid.property.Grid({ hidden: true });
        this.detailPanel = detailPanel;


        if (config == null)
            config = {};

        var cfg = {
            layout: "border",
            items: [
                {
                    region: "north",
                    xtype: 'panel',
                    title: "Search Panel",
                    collapsible: true,
                    collapsed: true,
                    layout: "fit",
                    items: searchPanel
                },
                {
                    region: "center",
                    xtype: 'panel',
                    title: "List",
                    layout: "fit",
                    items: grid
                },
                {
                    region: "south",
                    xtype: 'panel',
                    title: "Detail Panel",
                    collapsible: true,
                    collapsed: true,
                    layout: "fit",
                    items: [detailPanel, editPanel]
                }
            ]
        };

        Ext.apply(cfg, config);

        this.callParent([cfg]);
        return this;
    },


    showDetailPanel: function (entityId) {
        this.model.load(entityId, {
            success: function (entity) {
                this.detailPanel.setSource(entity);

                this.detailPanel.show();
                this.editPanel.hide();

                this.detailPanel.up("panel").expand();
            },
            failure: function () {
                alert("Error");
            }
        });
    },

    showEditPanel: function (entityId) {
        this.model.load(entityId, {
            success: function (entity) {
                this.editPanel.setSource(entity);

                this.detailPanel.hide();
                this.editPanel.show();

                this.editPanel.up("panel").expand();
            },
            failure: function () {
                alert("Error");
            }

        });
    }

});